package com.module.actor

import scala.actors.Actor

case class Msg(actor: Actor,msg:String)


class FirstActor extends Actor{
  def main(args: Array[String]): Unit = {

  }

  override def act(): Unit = {
    while (true){
      receive{
        case msg:Msg=>{
          if (msg.msg.equals("SecondActor:hello firstActor")){
            println(msg.msg)
            println("FirstActor:hello SecondActor")
            msg.actor ! "hi 太客气了"
          }else if(msg.msg.equals("你更客气啊")){
            println("FirstActor:哈哈哈，好吧")
          }
        }
        case i:String=>{println(i)}
      }
    }

  }
}
class SecondActor(a1:FirstActor) extends Actor{
  a1 ! Msg(this,"SecondActor:hello firstActor")
  def main(args: Array[String]): Unit = {

  }

  override def act(): Unit = {
    while (true){
      receive{
        case s:String=>{
          if (s.equals("hi 太客气了")){
            a1 ! Msg(this,"你更客气啊")
            println("SecondActor:有事先走了啊")
          }else{
            println("再见")
          }
        }
      }
    }

  }
}
object Actor2 {
  def main(args: Array[String]): Unit = {
    val a1=new FirstActor()
    a1.start()
    val a2=new SecondActor(a1)
    a2.start()
  }
}
